import json
import os

class ConfigManager:
    def __init__(self, config_file="config.json"):
        self.config_file = config_file
        self.config = {
            "ffmpeg_path": "D:\\bin\\ffmpeg\\bin\\ffmpeg.exe",
            "rtmp_url": "rtmp://10.31.32.47:1935/live/2",
            "video_device": "",
            "audio_device": ""
        }
        self.load_config()
    
    def load_config(self):
        """加载配置文件"""
        if os.path.exists(self.config_file):
            try:
                with open(self.config_file, 'r', encoding='utf-8') as f:
                    saved_config = json.load(f)
                    self.config.update(saved_config)
            except Exception as e:
                print(f"加载配置文件失败: {str(e)}")
    
    def save_config(self):
        """保存配置文件"""
        try:
            with open(self.config_file, 'w', encoding='utf-8') as f:
                json.dump(self.config, f, ensure_ascii=False, indent=2)
        except Exception as e:
            print(f"保存配置文件失败: {str(e)}")
    
    def update_config(self, key, value):
        """更新配置项"""
        if key in self.config:
            self.config[key] = value
            self.save_config()
    
    def get_config(self, key=None):
        """获取配置项，如果不指定key则返回所有配置"""
        if key is None:
            return self.config
        return self.config.get(key, None)